---
title: JSEncrypt
layout: default
---
<div class="starter-template">
  <h1>JSEncrypt</h1>
</div>
<div class="row">
  <div class="col-lg-12">
    <h3>Introduction</h3>
    <p>When browsing the internet looking for a good solution to RSA Javascript encryption, there is a whole slew of libraries that basically take the fantastic work done by Tom Wu @ <a href="http://www-cs-students.stanford.edu/~tjw/jsbn/">http://www-cs-students.stanford.edu/~tjw/jsbn/</a> and then modify that code to do what they want.</p>
    <p>What I couldn't find, however, was a simple wrapper around this library that basically uses the library <a href="https://github.com/travist/jsencrypt/pull/6">practically</a> untouched, but adds a wrapper to provide parsing of actual Private and Public key-pairs generated with OpenSSL.</p>
    <p>This library is the result of these efforts.</p>
    <div class="row col-lg-12">
      <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
      <!-- frontpage banner -->
      <ins class="adsbygoogle"
           style="display:inline-block;width:728px;height:90px"
           data-ad-client="ca-pub-1902035200208763"
           data-ad-slot="9043467163"></ins>
      <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
      </script>
    </div>
    <p>
      <a href="https://github.com/travist/jsencrypt/archive/master.zip" class="btn btn-primary btn-lg active" role="button">Download</a>
      <a href="https://github.com/travist/jsencrypt" class="btn btn-primary btn-lg active" role="button">Github Project</a>
    </p>
  </div>
</div>
<div class="row">
  <div class="col-lg-12">
    <h3>How to use this library.</h3><hr>
    <p>This library should work hand-in-hand with openssl.  With that said, here is how to use this library.</p>
    <ul>
      <li>Within your terminal (Unix based OS) type the following:</li>
    </ul>
    <pre><code>openssl genrsa -out rsa_1024_priv.pem 1024</code></pre>
    <ul>
      <li>This generates a private key, which you can see by doing the following...</li>
    </ul>
    <pre><code>cat rsa_1024_priv.pem #if you are on mac you can | pbcopy to copy to the clipboard</code></pre>
    <ul>
      <li>You can then copy and paste this in the Private Key section of the <a href="/jsencrypt/demo/">demo page</a>.</li>
      <li>Next, you can then get the public key by executing the following command.</li>
    </ul>
    <pre><code>openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem</code></pre>
    <ul>
      <li>You can see the public key by typing...</li>
    </ul>
    <pre><code>cat rsa_1024_pub.pem</code></pre>
    <ul>
      <li>Now you can compare with the one generated in the <a href="/jsencrypt/demo/">demo page</a>..</li>
      <li>Now you can then convert to and from encrypted text by doing the following in code.</li>
    </ul>
    <pre><code>// Create the encryption object and set the key.
var crypt = new JSEncrypt();
crypt.setKey(__YOUR_OPENSSL_PRIVATE_OR_PUBLIC_KEY__); //You can use also setPrivateKey and setPublicKey, they are both alias to setKey

//Eventhough the methods are called setPublicKey and setPrivateKey, remember
//that they are only alias to setKey, so you can pass them both a private or
//a public openssl key, just remember that setting a public key allows you to only encrypt.

var text = 'test';
// Encrypt the data with the public key.
var enc = crypt.encrypt(text);
// Now decrypt the crypted text with the private key.
var dec = crypt.decrypt(enc);

// Now a simple check to see if the round-trip worked.
if (dec === text){
    alert('It works!!!');
} else {
    alert('Something went wrong....');
}
    </code></pre>
    <ul><li>Look at the <a href="example.html">example page</a> to view this example in action.</li></ul>
  </div>
</div>
<div class="row">
  <div class="col-lg-12">
    <h3>Other Information</h3><hr>
    <p>This library heavily utilizes the wonderful work of Tom Wu found at <a href="http://www-cs-students.stanford.edu/~tjw/jsbn/">http://www-cs-students.stanford.edu/~tjw/jsbn/</a>.</p>
    <p>This jsbn library was written using the raw variables to perform encryption.  This is great for encryption, but most private keys use a Private Key in the PEM format seen below.</p>
    <h4>1024 bit RSA Private Key in Base64 Format</h4>
    <pre><code>-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDHikastc8+I81zCg/qWW8dMr8mqvXQ3qbPAmu0RjxoZVI47tvs
kYlFAXOf0sPrhO2nUuooJngnHV0639iTTEYG1vckNaW2R6U5QTdQ5Rq5u+uV3pMk
7w7Vs4n3urQ6jnqt2rTXbC1DNa/PFeAZatbf7ffBBy0IGO0zc128IshYcwIDAQAB
AoGBALTNl2JxTvq4SDW/3VH0fZkQXWH1MM10oeMbB2qO5beWb11FGaOO77nGKfWc
bYgfp5Ogrql4yhBvLAXnxH8bcqqwORtFhlyV68U1y4R+8WxDNh0aevxH8hRS/1X5
031DJm1JlU0E+vStiktN0tC3ebH5hE+1OxbIHSZ+WOWLYX7JAkEA5uigRgKp8ScG
auUijvdOLZIhHWq7y5Wz+nOHUuDw8P7wOTKU34QJAoWEe771p9Pf/GTA/kr0BQnP
QvWUDxGzJwJBAN05C6krwPeryFKrKtjOGJIniIoY72wRnoNcdEEs3HDRhf48YWFo
riRbZylzzzNFy/gmzT6XJQTfktGqq+FZD9UCQGIJaGrxHJgfmpDuAhMzGsUsYtTr
iRox0D1Iqa7dhE693t5aBG010OF6MLqdZA1CXrn5SRtuVVaCSLZEL/2J5UcCQQDA
d3MXucNnN4NPuS/L9HMYJWD7lPoosaORcgyK77bSSNgk+u9WSjbH1uYIAIPSffUZ
bti+jc1dUg5wb+aeZlgJAkEAurrpmpqj5vg087ZngKfFGR5rozDiTsK5DceTV97K
a3Y+Nzl+XWTxDBWk4YPh2ZlKv402hZEfWBYxUDn5ZkH/bw==
-----END RSA PRIVATE KEY-----
    </code></pre>
    <p>This library simply takes keys in the preceding format, and translates it to those variables needed to perform the encryptions used in Tom Wu's library.</p>
    <p>Here are some good resources to investigate further.</p>
    <ul>
      <li><a href="http://etherhack.co.uk/asymmetric/docs/rsa_key_breakdown.html">http://etherhack.co.uk/asymmetric/docs/rsa_key_breakdown.html</a></li>
      <li><a href="http://www.di-mgt.com.au/rsa_alg.html">http://www.di-mgt.com.au/rsa_alg.html</a></li>
      <li><a href="https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem">https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem</a></li>
    </ul>
    <p>With this information, we can translate a private key format to the variables required with the jsbn library from Tom Wu by using the following mappings.</p>
    <pre><code>modulus =&gt; n
public exponent =&gt; e
private exponent =&gt; d
prime1 =&gt; p
prime2 =&gt; q
exponent1 =&gt; dmp1
exponent2 =&gt; dmq1
coefficient =&gt; coeff
    </code></pre>
  </div>
</div>
